<?php

/**
 * 模型对应数据库表 "subsite".
 *
 * 以下是可用表subsite的栏位:
 * @property integer $id
 * @property string $site_key
 * @property string $domain
 * @property string $server_name
 * @property string $ipaddress
 * @property string $keywords
 * @property string $description
 * @property string $title
 */
class Subsite extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return Subsite the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_site';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('domain, oa_from_web, paymail, remit', 'required'),
            array('site_key', 'length', 'max' => 100),
            array('domain, ipaddress', 'length', 'max' => 50),
            array('server_name', 'length', 'max' => 120),
            array('link_phone,link_mail,paymail,keywords, description, title', 'length', 'max' => 225),
            array('remit,livechat', 'safe'),
            array('order_prefix', 'length', 'max' => 30),
            array('order_prefix', 'unique'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_key, domain, server_name, ipaddress, keywords, description, title, livechat, paymail, order_prefix, remit', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'site_key' => '网站Key',
            'domain' => '域名',
            'server_name' => '服务器名称',
            'ipaddress' => 'ip地址',
            'keywords' => 'SEO关键字',
            'description' => 'SEO关键字描述',
            'title' => 'SEO标题',
            'paymail' => '收款邮箱',
            'livechat' => '在线聊天',
            'order_prefix' => '订单前缀',
            'link_mail' => '联系邮箱',
            'link_phone' => '联系电话',
            'remit' => '西联汇款',
            'oa_from_web' => 'OA订单来源',
        );
    }

    /*
     *  获取域名 
     */

    public function getdomain() {
        $data = array();
        foreach ($this->findAll() as $v) {
            $data[$v->id] = $v->domain;
        }
        return $data;
    }

    /**
     * 获取多域名 
     */
    public function selectdomain($mString) {
        $sqlStr = $mString;
        
        if ($sqlStr) {
            $rString = "";
            $query = " id IN ($sqlStr) ";
            $mData = $this->findAll($query);
            foreach ($mData as $sData) {
                $rString.=$sData['domain'] . ",";
            }
            return trim($rString, ',');
        }

    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('site_key', $this->site_key);
        $criteria->compare('domain', $this->domain, true);
        $criteria->compare('server_name', $this->server_name, true);
        $criteria->compare('ipaddress', $this->ipaddress, true);
        $criteria->compare('keywords', $this->keywords, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('order_prefix', $this->order_prefix, true);
        $criteria->compare('remit', $this->remit, true);
        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    /**
      +-----------------------------------------------------
     * 说明 获取操作菜单
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getColumnOptions($id) {
        $vUrl = Yii::app()->controller->rewriteCreateUrl('subsite/view', array('id' => $id));
        $uUrl = Yii::app()->controller->rewriteCreateUrl('subsite/update', array('id' => $id));
        $dUrl = Yii::app()->controller->rewriteCreateUrl('subsite/delete', array('id' => $id));
        $sUrl = Yii::app()->controller->rewriteCreateUrl('subsiteCategory/admin', array('siteid' => $id));
        $view = '<a title="设置分类" href="' . $sUrl . '" class="gradient-btn">设置分类</a>';
        $view.= '<a title="查看" href="' . $vUrl . '" class="gradient-btn">查看</a>';
        $editor = '<a title="编辑" href="' . $uUrl . '" class="gradient-btn">编辑</a>';
        $cancel = '<a rel="subsite" title="删除" href="javascript:void(0)" durl ="' . $dUrl . '" onclick="deleteByUrl(this)" class="gradient-btn">删除</a>';
        return $view . $editor . $cancel;
    }

    /**
     * 获取站点列表
     * @return array 
     */
    public function getSiteSelect() {
        $cr = new CDbCriteria;
        $cr->order = "domain ASC";
        return CHtml::listData(self::findAll($cr), 'domain', 'domain');
    }

    /**
     * 获取订单前缀
     */
    public function getOrderPrefix() {
        $domain = Yii::app()->params['domain'];
        $site = $this->find("domain='$domain'");
        //print_r($site->attributes);
        //exit;
        if ($site) {
            return $site['order_prefix'];
        } else {
            return 'USF';
        }
    }

    /**
      +-----------------------------------------------------
     * 说明 西联订单信息
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getWebSiteInfo() {
        return $this->findByPk(Yii::app()->params['site_id']);
    }

    /**
     * 获取域名列表
     * @return type
     */
    public function getDomainSelect() {
        $cr = new CDbCriteria;
        $cr->order = "domain ASC";
        return CHtml::listData($this->findAll($cr), 'domain', 'domain');
    }

}
